﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.zhongjh.Library.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Log
    {
        #region 字段

        private string m_strFilePath = AppDomain.CurrentDomain.BaseDirectory + "LogFile.log";

        public string FilePath
        {
            get
            {
                Folder.CreateFolderPath(m_strFilePath);
                return m_strFilePath;
            }
            set { m_strFilePath = value; }
        }

        #endregion //字段

        #region 公有方法

        /// <summary>
        /// 构造函数
        /// </summary>
        public Log()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="p_strPath"></param>
        public Log(string p_strPath)
        {
            FilePath = p_strPath;
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="p_strLogContent"></param>
        public void WriteLog(string p_strLogContent)
        {
            using (StreamWriter sw = File.AppendText(FilePath))
            {
                string strTime = "\r\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]\r\n";

                //写文件
                sw.WriteLine(strTime + p_strLogContent);
            }
        }

        /// <summary>
        /// 写错误
        /// </summary>
        /// <param name="p_objErrorEntity"></param>
        public void WriteError(ErrorEntity p_objErrorEntity)
        {
            using (StreamWriter sw = File.AppendText(FilePath))
            {
                string strErrorContent = string.Empty;
                strErrorContent = "\r\n 系统编号:" + p_objErrorEntity.Id + 
                    "\r\n 错误编号:" + p_objErrorEntity.ErrorId + 
                    "\r\n 错误源:" + p_objErrorEntity.ErrorSource + 
                    "\r\n 系统错误信息:" + p_objErrorEntity.SysErrorMsy;
                //写文件
                sw.WriteLine(strErrorContent);
            }
        }

        #endregion //公有方法
    }
}
